package commmon.md5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 类描述：MD5加密类
 * <pre>
 * -------------History------------------
 *   DATE                 AUTHOR         VERSION        DESCRIPTION
 *                                     SUHUI         V01.00.001		   新增内容
 * </pre>
 *
 */
public class MD5 {

    public MD5(){

    }

    /**
     * MD5 Encryption method
     * method:getMD5ofStr
     * @param srcstr
     * @return a string after Encryption with MD5
     */
    public static String getMD5ofStr(String srcstr){
        MessageDigest messageDigest;
        String digestedPwdString="";
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(srcstr.getBytes());
            byte[] bin = messageDigest.digest();
            digestedPwdString=byte2hex(bin);
            digestedPwdString=digestedPwdString.toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            digestedPwdString="";
        }
        return digestedPwdString;
    }

    /**
     * Will be transferred to the array of characters labeled as a hexadecimal string
     * @param b
     * @return
     */
    private static String byte2hex(byte[] b){
        String hs="";
        String stmp="";
        for (int n=0; n<b.length; n++){
            stmp=(Integer.toHexString(b[n] & 0xFF));
            if (stmp.length()==1) {
                hs=hs+"0"+stmp;
            }
            else {
                hs=hs+stmp;
            }
        }
        return hs;
    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMD5(String inStr){

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++){
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;
    }

    public static void main(String[] args) {
		String s = "11";
		System.out.println("原始：" + s);
		System.out.println("MD5后：" + getMD5ofStr(s));
		System.out.println("解密的：" + convertMD5(convertMD5(s)));
    }

}

